package com.GraduationDesign.backend.blImpl.diff;

import com.GraduationDesign.backend.bl.diff.DiffService;
import com.GraduationDesign.backend.bl.project.ProjectService;
import com.GraduationDesign.backend.data.codeReview.ReviewMapper;
import com.GraduationDesign.backend.data.diff.DiffMapper;
import com.GraduationDesign.backend.data.project.ProjectMapper;
import com.GraduationDesign.backend.data.user.AccountMapper;
import com.GraduationDesign.backend.enums.ChangeFileType;
import com.GraduationDesign.backend.po.*;
import com.GraduationDesign.backend.utils.RequestUtil;
import com.GraduationDesign.backend.vo.CodeChangeInfo;
import com.GraduationDesign.backend.vo.ResponseVO;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.util.UriComponentsBuilder;


import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import static com.GraduationDesign.backend.constant.Constants.DEFAULT_OUTPUT_DIR;

/**
 * @author: wzx
 * @date: 2022/4/16
 */
@Service
public class DiffServiceImpl implements DiffService {
    @Autowired
    DiffMapper diffMapper;

    @Autowired
    ReviewMapper reviewMapper;

    @Autowired
    AccountMapper accountMapper;

    @Autowired
    ProjectService projectService;

    @Autowired
    ProjectMapper projectMapper;

    public static final String GET_DIFF_FILES = "http://172.31.105.39:8000/api/diff/getDiffFiles";
    @Override
    public DiffFileList getGroupList(Integer reviewId) {
        List<DiffFile> diffFileList=diffMapper.getDiffFileList(reviewId);
        return dealFile(diffFileList);
    }

    @Override
    public TempList getTempList(String path, Integer groupId,Integer reviewId) {
        TempList tempList=new TempList();
        List<BlockInfo> blockInfoList=diffMapper.getBlocks(path,groupId,reviewId);
        for(BlockInfo blockInfo:blockInfoList){
            Integer codeId=blockInfo.getCodeId();
            blockInfo.setFilePath(setNewFilePath(path,""));
            TempLink tempLink=diffMapper.getLink(reviewId,codeId);
            List<Comment> commentList=diffMapper.getComments(reviewId,codeId);
            if(commentList.size()>0){
            for(Comment comment:commentList){
                comment.setReviewerName(accountMapper.getUserName(reviewMapper.getReviewerId(reviewId)));
            }}
            blockInfo.setLink(tempLink);
            blockInfo.setComment(commentList);
            blockInfo.setLength(blockInfo.getEndLine()-blockInfo.getBeginLine());
        }
        Collections.sort(blockInfoList);
        tempList.setBlockInfoList(blockInfoList);
        return tempList;
    }



    @Override
    public String[] getDiffFiles(Integer reviewId, String path) {
        Review review = reviewMapper.getReview(reviewId);
        Project project = projectMapper.getProjectInfo(review.getProgramId());
        JSONObject jsonObject = JSONObject.parseObject(RequestUtil.getDiffFiles(GET_DIFF_FILES, project, review.getPullRequestId(), path).getBody());
        Map<String, String> data = jsonObject.getObject("data", Map.class);
        String[] files=new String[2];
//        path=DEFAULT_OUTPUT_DIR+path;
//        String oldpath=path.replace("curr","prev");
//        String newpath=path.replace("prev","curr");
//        files[0]=projectService.getFile(oldpath);
//        files[1]=projectService.getFile(newpath);
        files[0]=data.get("prev");
        files[1]=data.get("curr");
        return files;
    }

    @Override
    public LinkInfo getLinkInfo(Integer reviewId, Integer codeId) {
        List<LinkInfo> linkInfoList=diffMapper.getLinkInfo(reviewId,codeId);
        LinkInfo linkInfo=new LinkInfo();
        if(linkInfoList.size()==0) {
            return null;
        }
        if(linkInfoList.size()>1){
            for(LinkInfo linkInfo_:linkInfoList){
                if(linkInfo_.getFileType()== ChangeFileType.New){
                    linkInfo=linkInfo_;
                    break;
                }
            }
        }else{
            linkInfo=linkInfoList.get(0);
        }
        linkInfo.setFilePath(setNewFilePath(linkInfo.getFilePath(),""));
        return linkInfo;
    }

    @Override
    public ResponseVO updateCodeGroup(CodeChangeInfo codeChangeInfo) {
        diffMapper.updateCodeInfo(codeChangeInfo.getReviewId(),codeChangeInfo.getCodeId(),codeChangeInfo.getGroupId());
        return ResponseVO.buildSuccess();
    }



    public String setNewFileName(String path){
        String[] word=path.split("/");
        return word[word.length-1];
    }
    public String setNewFilePath(String path,String filename){
        path=path.replace(DEFAULT_OUTPUT_DIR,"");
        path=path.replace(filename,"");
        return path;
    }
    public DiffFileList dealFile(List<DiffFile> diffFileList){
        DiffFileList diffFileListArrayList=new DiffFileList(new ArrayList<>());
        List<DiffFile> diffFiles=new ArrayList<>();
        int group=diffFileList.get(0).getGroup();
        boolean flag=true;
        List<String> fileList = new ArrayList<>();
        for(DiffFile diffFile:diffFileList){
            int group1=diffFile.getGroup();
            if(group1!=group){
                group=group1;
                diffFileListArrayList.getFileList().add(diffFiles);
                diffFiles=new ArrayList<>();
                flag=true;
                fileList.clear();
            }
            String filename = setNewFileName(diffFile.getFilepath());
            if (!fileList.contains(filename)){
                fileList.add(filename);
                diffFile.setFilename(filename);
                diffFile.setFilepath(setNewFilePath(diffFile.getFilepath(),diffFile.getFilename()));
                diffFiles.add(diffFile);
                flag=false;
            }
        }
        diffFileListArrayList.getFileList().add(diffFiles);
        return diffFileListArrayList;
    }

}
